package com.hellogood.yaohuo.client.bussiness.yaohuo;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hellogood.yaohuo.client.common.CommonMessages;
import com.hellogood.yaohuo.shared.data.Constants;
import com.hellogood.yaohuo.shared.entity.YhForm;
import com.hellogood.yaohuo.shared.services.YaohuoService;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.util.BooleanCallback;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.DoubleClickEvent;
import com.smartgwt.client.widgets.events.DoubleClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;
import com.smartgwt.client.widgets.toolbar.ToolStrip;

public class YaohuoTab extends Tab {
	private TabSet tabSet = null;
	private Button newBtn = null;
	private Button modifyBtn = null;
	private Button saveBtn = null;
	private Button cancelBtn = null;
	private Button deleteBtn = null;
	private Button bankBtn = null;
	private SearchTab searchTab = null;
	private FormTab formTab = null;

	private ListGrid searchListGrid = null;

	public YaohuoTab() {
		initView();
	}

	private void initView() {
		searchTab = new SearchTab();
		searchTab.setTitle(Constants.SEARCH);
		formTab = new FormTab();
		formTab.setTitle(Constants.FORM);

		tabSet = new TabSet();
		tabSet.addTab(searchTab);
		tabSet.addTab(formTab);

		searchListGrid = searchTab.getSearchController().getListGrid();
		searchListGrid.addDoubleClickHandler(new RecordDoubleClickHandler());

		newBtn = new Button(Constants.BUTTON_NEW);
		newBtn.addClickHandler(new NewBtnClickHandler());
		modifyBtn = new Button(Constants.BUTTON_MODIFY);
		modifyBtn.setDisabled(true);
		modifyBtn.addClickHandler(new ModifyBtnClickHandler());
		saveBtn = new Button(Constants.BUTTON_SAVE);
		saveBtn.setDisabled(true);
		saveBtn.addClickHandler(new SaveBtnClickHandler());
		cancelBtn = new Button(Constants.BUTTON_CANCEL);
		cancelBtn.setDisabled(true);
		cancelBtn.addClickHandler(new CancelBtnClickHandler());
		deleteBtn = new Button(Constants.BUTTON_DELETE);
		deleteBtn.setDisabled(true);
		deleteBtn.addClickHandler(new DeleteBtnClickHandler());
		bankBtn = new Button(Constants.BUTTON_BANK);
		bankBtn.setDisabled(true);
		bankBtn.addClickHandler(new BankBtnClickHandler());
		ToolStrip toolStrip = new ToolStrip();
		toolStrip.setMembers(newBtn, modifyBtn, saveBtn, cancelBtn, deleteBtn, bankBtn);
		toolStrip.setMembersMargin(20);
		VLayout layout = new VLayout();
		layout.addMember(toolStrip);
		layout.addMember(tabSet);
		layout.setMembersMargin(10);

		setCanClose(true);
		setPane(layout);
	}

	private class RecordDoubleClickHandler implements DoubleClickHandler {

		@Override
		public void onDoubleClick(DoubleClickEvent event) {
			Record record = searchListGrid.getSelectedRecord();
			if (record != null) {
				String billNo = record.getAttribute("code");
				if (billNo != null) {
					YaohuoService.Util.getInstance().getFormDetail(billNo, new YaohuoAsyncCallback());
				}
			}
		}
	}

	private class YaohuoAsyncCallback implements AsyncCallback<YhForm> {

		@Override
		public void onFailure(Throwable caught) {
			SC.say(caught.getMessage());
		}

		@Override
		public void onSuccess(YhForm result) {
			if (result != null) {
				formTab.getFormController().updateForm(result);
				tabSet.selectTab(formTab);
			}
		}
	}

	private class NewBtnClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			tabSet.selectTab(formTab);
			formTab.getFormController().getListGrid().setDisabled(false);
			formTab.getFormController().initForm();
			showCreateBill();
		}
	}

	private class ModifyBtnClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			tabSet.selectTab(formTab);
			formTab.getFormController().getListGrid().setDisabled(false);
			showModifyBill();
		}
	}

	private class SaveBtnClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			tabSet.selectTab(formTab);
			formTab.getFormController().getListGrid().setDisabled(false);
			formTab.getFormController().saveForm();
			showSaveSuccessful();
		}
	}

	private class CancelBtnClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			formTab.getFormController().cancelForm();
			showRzSuccessful();
		}
	}

	private class DeleteBtnClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			SC.confirm(CommonMessages.ConfirmDelete, new BooleanCallback() {

				@Override
				public void execute(Boolean value) {
					if (value != null && value) {
						formTab.getFormController().deleteForm();
						showRzSuccessful();
					}
				}
			});
		}
	}

	private class BankBtnClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			SC.confirm(CommonMessages.ConfirmDelete, new BooleanCallback() {

				@Override
				public void execute(Boolean value) {
					if (value != null && value) {
						formTab.getFormController().bankForm();
						showRzSuccessful();
					}
				}
			});
			showRzSuccessful();
		}
	}

	public void showCreateBill() {
		newBtn.setDisabled(true);
		modifyBtn.setDisabled(true);
		saveBtn.setDisabled(false);
		cancelBtn.setDisabled(false);
		deleteBtn.setDisabled(true);
		bankBtn.setDisabled(true);
	}

	public void showUnRzBill() {
		newBtn.setDisabled(false);
		modifyBtn.setDisabled(false);
		saveBtn.setDisabled(true);
		cancelBtn.setDisabled(true);
		deleteBtn.setDisabled(false);
		bankBtn.setDisabled(false);
	}

	public void showRzBill() {
		newBtn.setDisabled(false);
		modifyBtn.setDisabled(true);
		saveBtn.setDisabled(true);
		cancelBtn.setDisabled(true);
		deleteBtn.setDisabled(true);
		bankBtn.setDisabled(true);
	}

	public void showModifyBill() {
		newBtn.setDisabled(true);
		modifyBtn.setDisabled(true);
		saveBtn.setDisabled(false);
		cancelBtn.setDisabled(false);
		deleteBtn.setDisabled(false);
		bankBtn.setDisabled(true);
	}

	public void showSaveSuccessful() {
		newBtn.setDisabled(false);
		modifyBtn.setDisabled(false);
		saveBtn.setDisabled(true);
		cancelBtn.setDisabled(true);
		deleteBtn.setDisabled(false);
		bankBtn.setDisabled(false);
	}

	public void showRzSuccessful() {
		newBtn.setDisabled(false);
		modifyBtn.setDisabled(true);
		saveBtn.setDisabled(true);
		cancelBtn.setDisabled(true);
		deleteBtn.setDisabled(true);
		bankBtn.setDisabled(true);
	}

}
